package com.epro.standard.usermenu.manage;

import com.softplus.carrefour.CFException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import com.epro.standard.StandardException;
import com.epro.standard.frontend.lov.EproLOVRow;
import com.epro.standard.frontend.lov.LOVKeyword;
import com.epro.standard.infrastructure.dataaccess.DBManage;
import com.epro.standard.infrastructure.master.MasterManage;
import com.epro.standard.usermenu.RoleEntity;
import com.epro.standard.usermenu.RoleKeyword;
import com.epro.standard.usermenu.RoleSortKeyword;
import com.epro.standard.usermenu.RoleVO;

import com.softplus.carrefour.hd.bltier.util.LOVtemplate;

public class RoleManage {
    private DBManage dbm = new DBManage();

    /**
   @roseuid 3EA8E5B8025B
     */
    public RoleManage() {

    }

    private void manager(int dbType, 
                         RoleEntity roleEntity) throws StandardException {
        MasterManage ct = new MasterManage();
        switch (dbType) {
        case DBManage.INSERT:
            ct.insert(roleEntity);
            break;
        case DBManage.UPDATE:
            ct.update(roleEntity);
            break;
        case DBManage.DELETE:
            ct.delete(roleEntity);
            break;
        case DBManage.INSERT_ID_GENERATOR:
            ct.insertIDGenerator(roleEntity);
            break;
        }
    }

    /**
   @param roleEntity
   @roseuid 3EA8DF3B0111
     */
    public void addRole(RoleEntity roleEntity) throws StandardException {
        roleEntity.setUpdDate(new com.epro.standard.infrastructure.util.Timestamp(System.currentTimeMillis(), 
                                                                                  true));
        roleEntity.setCredDate(new com.epro.standard.infrastructure.util.Timestamp(System.currentTimeMillis(), 
                                                                                   true));
        manager(DBManage.INSERT, roleEntity);
    }

    /**
   @param roleEntity
   @roseuid 3EA8DF3B0112
     */
    public void editRole(RoleEntity roleEntity) throws StandardException {
        roleEntity.setUpdDate(new com.epro.standard.infrastructure.util.Timestamp(System.currentTimeMillis(), 
                                                                                  true));
        manager(DBManage.UPDATE, roleEntity);
    }

    /**
   @param roleEntity
   @roseuid 3EA8DF3B0113
     */
    public void removeRole(RoleEntity roleEntity) throws StandardException {

        manager(DBManage.DELETE, roleEntity);
    }

    /**
   @return java.util.Vector
   @roseuid 3EA8DF3B0114
     */
    public Vector getAllRole() throws StandardException {
        RoleKeyword roleKeyword = new RoleKeyword();
        roleKeyword.addOrderAsc(roleKeyword.getR_roleID());

        return dbm.executeQuery(roleKeyword);
    }

    public Vector getAllRole(RoleSortKeyword roleSortKeyword, 
                             boolean isAsc) throws StandardException {
        RoleKeyword roleKeyword = new RoleKeyword();

        if (roleSortKeyword.isRoleID()) {
            if (isAsc)
                roleKeyword.addOrderAsc(roleKeyword.getR_roleID());
            else
                roleKeyword.addOrderDesc(roleKeyword.getR_roleID());
        } else if (roleSortKeyword.isRoleName()) {
            if (isAsc)
                roleKeyword.addOrderAsc(roleKeyword.getR_roleName());
            else
                roleKeyword.addOrderDesc(roleKeyword.getR_roleName());
        }
        if (roleSortKeyword.isRoleDesc()) {
            if (isAsc)
                roleKeyword.addOrderAsc(roleKeyword.getR_roleDesc());
            else
                roleKeyword.addOrderDesc(roleKeyword.getR_roleDesc());
        }
        if (roleSortKeyword.isActive()) {
            if (isAsc)
                roleKeyword.addOrderAsc(roleKeyword.getR_active());
            else
                roleKeyword.addOrderDesc(roleKeyword.getR_active());
        }
        if (roleSortKeyword.isUpdBy()) {
            if (isAsc)
                roleKeyword.addOrderAsc(roleKeyword.getR_updBy());
            else
                roleKeyword.addOrderDesc(roleKeyword.getR_updBy());
        }
        if (roleSortKeyword.isUpdDate()) {
            if (isAsc)
                roleKeyword.addOrderAsc(roleKeyword.getR_updDate());
            else
                roleKeyword.addOrderDesc(roleKeyword.getR_updDate());
        }

        return dbm.executeQuery(roleKeyword);
    }

    public Vector getLOVRole() throws StandardException {
        RoleKeyword roleKeyword = new RoleKeyword();
        roleKeyword.addCommonCriteria(roleKeyword.getR_active(), "Y");
        roleKeyword.addOrderAsc(roleKeyword.getR_roleID());

        return dbm.executeQuery(roleKeyword);
    }

    public RoleVO getRole(String roleID) throws StandardException {
        RoleKeyword roleKeyword = new RoleKeyword();
        roleKeyword.addCommonCriteria(roleKeyword.getR_roleID(), 
                                      roleID.trim());
        roleKeyword.addOrderAsc(roleKeyword.getR_roleID());

        Vector v = dbm.executeQuery(roleKeyword);
        return (RoleVO)v.elementAt(0);
    }
    //   public Vector getLOVRole(LOVKeyword key) throws StandardException {
    //       Vector v = new Vector();
    //       
    //       String searchValue = key.getMessage();
    //	   	Vector checkBoxs = key.getCheckBox();
    //    	HashMap conds = key.getCondition();
    //	         	
    //	   	if(checkBoxs.size() == 0) {  
    //	   	    checkBoxs.addElement("0");
    //	   	}
    //	   	
    //	   	HashMap serachCols = new HashMap();
    //	   	
    //	   	serachCols.put("0"," UPPER(R.ROLE_ID) ");
    //	   	serachCols.put("1"," UPPER(R.ROLE_NAME) ");
    //
    //     	String inCountryCondition = "";
    //     	String betweenRIDCondition = "";
    //     	
    //     	String inCntry = (String) conds.get("countryID");
    //     	String resourceID1 = (String) conds.get("resourceID1");
    //     	String resourceID2 = (String) conds.get("resourceID2");
    //     	
    //     	
    //     	if(inCntry != null) {
    //     	   inCountryCondition = " and U.COUNTRY_ID = '"+inCntry+"' ";
    //     	}
    //    	if(resourceID1 != null) {
    //    	    betweenRIDCondition = " and U.ROLE_ID = '"+resourceID1+"' "; 
    //     	}
    //    	if(resourceID2 != null) {
    //    	    betweenRIDCondition = " and U.ROLE_ID = '"+resourceID1+"' "; 
    //     	}	   	
    //
    //	   	
    //	    String orderBy = " order by R.ROLE_ID ";
    //	   	String sql = "select  " +
    //	   		"R.ROLE_ID as R_ROLE_ID,"+
    //	   		"R.UPD_DATE as R_UPD_DATE,"+
    //	   		"R.ROLE_NAME as R_ROLE_NAME,"+
    //	   		"R.CRED_BY as R_CRED_BY,"+
    //	   		"R.CRED_DATE as R_CRED_DATE,"+
    //	   		"R.ACTIVE as R_ACTIVE,"+
    //	   		"R.ROLE_DESC as R_ROLE_DESC,"+
    //	   		"R.UPD_BY as R_UPD_BY"+
    //	   		" from ROLE R " +
    //	   		" where R.ACTIVE = 'Y' and (";
    //	   	
    //	   	String searchCondEqual = "  R.ROLE_ID = '"+ searchValue  +"')";
    //	   	
    //	   	System.out.println(sql+searchCondEqual);
    //	   	Vector res = dbm.executeQuery(new RoleKeyword(),sql+searchCondEqual);
    //	   	if(res.size() == 1){
    //	   	    RoleVO rs = (RoleVO) res.elementAt(0);
    //	   	    v.addElement(new EproLOVRow(rs.getR_ROLE_ID(),rs.getR_ROLE_NAME()));
    //	   	}else{
    //	    	
    //	   	    String searchCondLike = "";
    //	    	for (Iterator iter = checkBoxs.iterator(); iter.hasNext();) {
    //               	String cond = (String)iter.next();    
    //   	    	    String orConcat = "";
    //   	    	    if(iter.hasNext())
    //   	    	        orConcat = " or ";
    //               	searchCondLike += "("+ serachCols.get(cond) +" like '"+searchValue.toUpperCase()+"%')" + orConcat;   
    //               }	    	
    //	    	sql += searchCondLike+")"+orderBy;
    //	    	System.out.println(sql);
    //	    	res = dbm.executeQuery(new RoleKeyword(),sql);
    //	    	
    //	    	for (Iterator iter = res.iterator(); iter.hasNext();) {	    	    
    //	    	    RoleVO rs = (RoleVO) iter.next();
    //	    	    v.addElement(new EproLOVRow(rs.getR_ROLE_ID(),rs.getR_ROLE_NAME()));
    //           }
    //
    //   	}
    //       return v;       
    //   }

    public Vector getLOVRole(LOVKeyword key) throws CFException, 
                                                    StandardException {

        String searchValue = key.getMessage();
        Vector checkBoxs = key.getCheckBox();
        HashMap conds = key.getCondition();

        if (checkBoxs.size() == 0) {
            checkBoxs.addElement("0");
        }

        HashMap serachCols = new HashMap();

        serachCols.put("0", " RO.ROLE_ID ");
        serachCols.put("1", " RO.ROLE_NAME ");

        String inCountryCondition = "";
        String resourcesCondition = "";
        String inCntry = "";
        String resourceID1 = "";
        String resourceID2 = "";
        String allConditions = "";

        boolean r1 = false;
        boolean r2 = false;

        if (conds != null) {
            inCntry = (String)conds.get("countryID");
            resourceID1 = (String)conds.get("resourceID1");
            resourceID2 = (String)conds.get("resourceID2");
        }

        if ((inCntry != null) && !(inCntry.compareTo("") == 0)) {
            inCountryCondition = " and (UA.COUNTRY_ID = '" + inCntry + "') ";
        }

        if ((resourceID1 != null) && !(resourceID1.compareTo("") == 0)) {
            r1 = true;
        }
        if ((resourceID2 != null) && !(resourceID2.compareTo("") == 0)) {
            r2 = true;
        }

        if (r1 && r2) {
            resourcesCondition = 
                    " and (RS.RESOURCE_ID BETWEEN '" + resourceID1 + 
                    "' and '" + resourceID2 + "') ";
        } else if (r1) {
            resourcesCondition = 
                    " and (RS.RESOURCE_ID >= '" + resourceID1 + "') ";
        } else if (r2) {
            resourcesCondition = 
                    " and (RS.RESOURCE_ID <= '" + resourceID2 + "') ";
        }

        allConditions = inCountryCondition + resourcesCondition;
        String sql = "";
        if (allConditions.compareTo("") != 0) {
            sql = 
"select distinct " + "RO.ROLE_ID as ROLE_ID," + "RO.ROLE_NAME as ROLE_NAME " + 
  "from ROLE RO , " + "RESOURCES RS ," + "USER_ACCOUNT UA " + 
  "where RO.ACTIVE = 'Y'  " + "and (RO.ROLE_ID = UA.ROLE_ID) " + 
  "and (RS.USER_ID = UA.USER_ID) " + "and (";
        } else {
            sql = 
"select " + "RO.ROLE_ID as ROLE_ID," + "RO.ROLE_NAME as ROLE_NAME " + 
  "from ROLE RO  " + "where RO.ACTIVE = 'Y'  " + "and (";
        }
        return new LOVtemplate().getLOV(sql, 
                                        new String[] { "ROLE_ID", "ROLE_NAME" }, 
                                        searchValue, serachCols, checkBoxs, "", 
                                        allConditions);
    }

    public Vector getLOVRoleAll(LOVKeyword key) throws CFException, 
                                                       StandardException {
        String searchValue = key.getMessage();
        Vector checkBoxs = key.getCheckBox();

        HashMap serachCols = new HashMap();

        if (checkBoxs.size() == 0) {
            checkBoxs.addElement("0");
        }


        serachCols.put("0", "ROLE_ID");
        serachCols.put("1", "ROLE_NAME");


        String sql = 
            "select " + "ROLE_ID," + "ROLE_NAME " + "from ROLE " + "where ACTIVE='Y'" + 
            " and (";

        return new LOVtemplate().getLOV(sql, 
                                        new String[] { "ROLE_ID", "ROLE_NAME" }, 
                                        searchValue, serachCols, checkBoxs, "", 
                                        "");
    }

    public Vector getLOVRoleByName(LOVKeyword key) throws CFException, 
                                                          StandardException {
        String searchValue = key.getMessage();
        Vector checkBoxs = key.getCheckBox();

        HashMap serachCols = new HashMap();

        if (checkBoxs.size() == 0) {
            checkBoxs.addElement("1");
        }


        serachCols.put("0", "ROLE_ID");
        serachCols.put("1", "ROLE_NAME");


        String sql = 
            "select " + "ROLE_ID," + "ROLE_NAME " + "from ROLE " + "where ACTIVE='Y'" + 
            " and (";

        return new LOVtemplate().getLOV(sql, 
                                        new String[] { "ROLE_ID", "ROLE_NAME" }, 
                                        searchValue, serachCols, checkBoxs, "", 
                                        "");
    }
}
